﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SvnTools
{
	/// <summary>
	/// This class is used to hold results of command 'svnlook info'.
	/// </summary>
	public class SvnLookInfo
	{
		const int IdxCommitAuthor = 0;
		const int IdxCommitTime = 1;
		const int IdxLogMessageLength = 2;
		const int IdxLogMessage = 3;

		private string m_CommitAuthor;
		private string m_CommitTimeStr;
		private DateTime m_CommitTime;
		private int m_LogMessageLength;
		private string m_LogMessage;

		public SvnLookInfo()
		{
			m_CommitAuthor = "";
			m_CommitTimeStr = "";
			m_CommitTime = DateTime.MinValue;
			m_LogMessageLength = 0;
			m_LogMessage = "";
		}

		#region Parse methods.

		/// <summary>
		/// Parse from a string array.
		/// </summary>
		/// <param name="lines">The result of 'svnlook info' stored in a string array.</param>
		/// <returns>A SvnLookInfo object.</returns>
		public static SvnLookInfo Parse(string[] lines)
		{
			if (lines.Length < 4)
			{
				throw new Exception("SvnLookInfo.Parse() failed: the result of 'svnlook info' should have 4 lines.");
			}

			SvnLookInfo lookInfo = new SvnLookInfo();
			try
			{
				lookInfo.CommitAuthor = lines[IdxCommitAuthor];
				lookInfo.m_CommitTimeStr = lines[IdxCommitTime];
				lookInfo.CommitTime = DateTime.ParseExact(lookInfo.m_CommitTimeStr.Substring(0, 19), 
					"yyyy-MM-dd HH:mm:ss",	null, System.Globalization.DateTimeStyles.AdjustToUniversal);
				lookInfo.LogMessageLength = Convert.ToInt32(lines[IdxLogMessageLength]);
				lookInfo.LogMessage = lines[IdxLogMessage];

				// for debugging.
				//File.WriteAllText(@"C:\SvnLogInfo.output", lookInfo.ToString());

				return lookInfo;
			}
			catch (Exception ex)
			{
				throw new Exception("SvnLookInfo.Parse() failed: " + ex.ToString());
			}
		}

		public static SvnLookInfo Parse(StreamReader reader)
		{
			string[] lines = new string[4];
			for (int i = 0; i < lines.Length; i++)
			{
				lines[i] = reader.ReadLine();
			}
			return SvnLookInfo.Parse(lines);
		}

		/// <summary>
		/// Read 'svnlook info' results from console.
		/// </summary>
		public static SvnLookInfo ParseStandardInput()
		{
			using (StreamReader reader = new StreamReader(Console.OpenStandardInput(), Encoding.Default))
			{
				return SvnLookInfo.Parse(reader);
			}
		}

		#endregion

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool HasLogMessage()
		{
			if (m_LogMessageLength < 1)
				return false;
			if (String.IsNullOrEmpty(m_LogMessage.Trim()))
				return false;
			return true;
		}

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			sb.Append(m_CommitAuthor);
			sb.Append(Environment.NewLine);
			sb.Append(m_CommitTimeStr);
			sb.Append(Environment.NewLine);
			sb.Append(m_LogMessageLength.ToString());
			sb.Append(Environment.NewLine);
			sb.Append(m_LogMessage);

			return sb.ToString();
		}

		#region Properties

		public string CommitAuthor
		{
			get { return m_CommitAuthor; }
			set { m_CommitAuthor = value; }
		}

		public DateTime CommitTime
		{
			get { return m_CommitTime; }
			set { m_CommitTime = value; }
		}

		public int LogMessageLength
		{
			get	{ return m_LogMessageLength; }
			set { m_LogMessageLength = value; }
		}

		public string LogMessage
		{
			get { return m_LogMessage; }
			set { m_LogMessage = value; }
		}

		#endregion


	}
}
